package cn.tx.erp.sys.controller;

import cn.tx.erp.sys.domain.Dept;
import cn.tx.erp.sys.service.DeptService;
import cn.tx.erp.sys.tree.TreeNode;
import cn.tx.erp.sys.utils.DataGridView;
import cn.tx.erp.sys.utils.ResultObj;
import cn.tx.erp.sys.vo.DeptVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/2/6 22:20
 */
@RestController
@RequestMapping("dept")
@Api(tags = "部门管理")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 加载部门管理左侧的部门树
     * @return
     */
    @ApiOperation(value = "加载部门管理左侧的部门树")
    @RequestMapping(value = "loadDeptJson")
    //@RequestMapping(value = "loadDeptJson",method = RequestMethod.POST)
    public DataGridView loadDeptLeftJson(DeptVo deptVo) {
        List<Dept> list = this.deptService.list();
        List<TreeNode> nodeList = new ArrayList<>();

        for (Dept dept : list) {
            Boolean spread = dept.getOpen() == 1 ? true : false;
            nodeList.add(new TreeNode(dept.getId(), dept.getPid(), dept.getTitle(), spread));

        }
        return new DataGridView(0, "", 0L, nodeList);
    }

    /**
     * 查询部门信息
     * @param deptVo
     * @return
     */
    @ApiOperation(value = "查询部门信息")
    @RequestMapping(value = "loadAllDept", method = RequestMethod.GET)
    public DataGridView loadAllDept(DeptVo deptVo) {
        // 分页
        IPage page = new Page(deptVo.getPage(), deptVo.getLimit());

        // 构造条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getTitle()), "title", deptVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getRemark()), "remark", deptVo.getRemark());
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getAddress()), "address", deptVo.getAddress());

        // 条件 查询
        queryWrapper.eq(deptVo.getId() != null, "pid", deptVo.getId());
        //queryWrapper.eq(deptVo.getId()!=null, "id", deptVo.getId()).or().eq(deptVo.getId()!=null,"pid", deptVo.getId());

        // 排序字段  排序吗
        queryWrapper.orderByAsc("ordernum");


        this.deptService.page(page, queryWrapper);
        return new DataGridView(0, "", page.getTotal(), page.getRecords());
    }

    /**
     * 添加
     * @param deptVo
     * @return
     */
    @ApiOperation(value = "添加部门信息")
    @RequestMapping("addDept")
    public ResultObj addDept(DeptVo deptVo) {
        try {
            //deptService.setType(Constast.TYPE_MENU);//设置添加类型
            deptVo.setCreatetime(new Date());
            this.deptService.save(deptVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERR;
        }
    }


    /**
     * 修改
     * @param deptVo
     * @return
     */
    @ApiOperation(value = "修改部门信息")
    @RequestMapping("updateDept")
    public ResultObj updateMenu(DeptVo deptVo) {
        try {
            this.deptService.updateById(deptVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERR;
        }
    }

    /**
     * 检查当前id 的部门有没有子部门
     * @param deptVo
     * @return
     */
    @ApiOperation(value = "检查当前id 的部门有没有子部门")
    @RequestMapping("checkDeptHasChildrenNode")
    public Map<String, Object> checkDeptHasChildrenNode(DeptVo deptVo) {
        Map<String, Object> map = new HashMap<>();

        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.eq("pid", deptVo.getId());

        List<Dept> list = this.deptService.list(qw);
        if (list.size() > 0) {
            map.put("value", true);
        } else {
            map.put("value", false);
        }
        return map;
    }

    /**
     * 删除
     * @param deptVo
     * @return
     */
    @ApiOperation(value = "删除部门信息")
    @RequestMapping("deleteDept")
    public ResultObj deleteDept(DeptVo deptVo) {
        try {
            this.deptService.removeById(deptVo.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERR;
        }
    }
}
